<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

JFX::checkAdminPermsRedirect('core', 'perm_manage_users');


$CONFIG->debug = true;


$VIEW->template = 'admin-thickbox.tpl';



$userid = (int) get('user');

if($DB->countRows($CONFIG->dbprefix.'admin_users', "id = '{$userid}'")==0){
    echo '<script type="text/javascript">window.parent.tb_remove()</script>';
}

if(Antz_IntelliForm::submitted('edit-be-user')){
    $error = false;
    $username = post('username');
    $email = post('email');
    $name = post('name');
    $password = post('password');
    $password2 = post('password');

    if($DB->countRows($CONFIG->dbprefix.'admin_users', "username = '{$username}' AND id != '{$userid}'")>0){
        $error = true;
        JFX::addError('Username is already registered');
        JFX::addFormError('username');
    }
    if($password != '' && $password2 != ''){
        if($password != $password2){
            $error = true;
            JFX::addError('Password confirmation does not match');
            JFX::addFormError('confPassword');
        }
        if(strlen($password)<6){
            $error = true;
            JFX::addError('Password must be greater than 6 characters');
            JFX::addFormError('password');
        }
    }

    if(!$error){
        $dbParams = array(
            'username' => $username,
            'email' => $email,
            'full_name' => $name
        );
        if($password != ''){
            $dbParams['pass'] = $CRYPT->crypt($password);
        }
        $DB->update($CONFIG->dbprefix.'admin_users', $dbParams, "id = '{$userid}'");
        JFX::addSuccess('Updated ok');
    }

}



$uData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}admin_users WHERE id = '{$userid}'");


$editFormFields = array(
    array(
        'name' => 'username',
        'type' => 'text',
        'value' => post('username', $uData['username']),
        'label' => 'Username'
    ),
    array(
        'name' => 'name',
        'type' => 'text',
        'value' => post('name', $uData['full_name']),
        'label' => 'Name'
    ),
    array(
        'name' => 'email',
        'type' => 'text',
        'value' => post('email', $uData['email']),
        'class' => 'wide',
        'label' => 'Email'
    ),
    array(
        'name' => 'password',
        'type' => 'password',
        'value' => '',
        'label' => 'New Password'
    ),
    array(
        'name' => 'confPassword',
        'type' => 'password',
        'label' => 'Confirm Password',
        'value' => ''
    ),
    array(
        'name' => 'submitBtn',
        'type' => 'submit',
        'value' => 'Update User',
        'label' => '&nbsp;'
    )
);


$editForm = JFX::makeRapidForm($editFormFields, 'Edit backend user', 'edit-be-user');

echo $editForm->render();






